package com.ssyt.hiring.talent.service;

import java.util.List;

import org.apache.commons.lang3.Validate;

import com.ssyt.hiring.common.util.DBUtil;
import com.ssyt.hiring.talent.entity.TalentEntity;

public class TalentService {

	/**
	 * 求职者登录
	 * 
	 * @param code
	 * @param password
	 * @return
	 */
	public TalentEntity login(String code, String password) {
		Validate.notBlank(code, "参数code不能为空", code);
		Validate.notBlank(password, "参数password不能为空", password);
		return DBUtil.getUniqueResult(TalentEntity.class,
				"SELECT * FROM talent WHERE code=? AND password=? LIMIT 1",
				code, password);
	}

	/**
	 * 求职者注册
	 * 
	 * @param list
	 * @return
	 */
	public int add(List<Object> list) {
		String sql = "INSERT INTO talent values(NUll,?,?,?,?,?,?,?,?)";
		return DBUtil.executeUpdate(sql, list);
	}

	/**
	 * 修改前按id 查询
	 * 
	 * @param id
	 * @return
	 */
	public TalentEntity preUpdate(int id) {
		String sql = "SELECT * FROM talent WHERE id=?";
		return DBUtil.getUniqueResult(TalentEntity.class, sql, id);
	}

	/**
	 * 修改求职者信息
	 * 
	 * @param list
	 * @return
	 */
	public int update(List<Object> list) {
		String sql = "UPDATE talent SET name=?,phone=?,email=?,age=?,gender=?,hobby=? WHERE id=?";
		return DBUtil.executeUpdate(sql, list);
	}

	/**
	 * 修改密码
	 * 
	 * @param oldPassword
	 * @param newPassword
	 * @return
	 */
	public int updatePwd(String oldPassword, String newPassword, int id) {
		String sql = "UPDATE talent SET password=? WHERE id=? AND password=?";
		return DBUtil.executeUpdate(sql, newPassword, id, oldPassword);
	}

	/**
	 * 按帐号查询信息
	 * 
	 * @param code
	 * @return
	 */
	public TalentEntity queryByCode(String code) {
		Validate.notBlank(code, "参数code不能为空", code);
		return DBUtil.getUniqueResult(TalentEntity.class,
				"SELECT * FROM talent WHERE code=? LIMIT 1", code);
	}

	/**
	 * 根据id 查询求职者信息
	 * 
	 * @param id
	 * @return
	 */
	public TalentEntity queryById(int id) {
		String sql = "SELECT * FROM talent WHERE id=?";
		return DBUtil.getUniqueResult(TalentEntity.class, sql, id);
	}

}
